% Log likelihood to obtain bidder parameters
% ---------------------------------------------- %
function[LL] = bidderLL(theta,lambda,data,PAR) 
   if PAR.generalized == 0 %Gaussian, or other two-parameter distribution    
        db  = makedist(PAR.dist,theta(1),theta(2));
        CDF = @(x) cdf(db,x);
        PDF = @(x) pdf(db,x);
  else %Generalized gaussian
        CDF = @(x) ggd_cdf(x,theta);
        PDF = @(x) ggd_pdf(x,theta);
  end
    
    try
        % prob sh value given actual bidders >=2
        idx = data.numberbidders>=2&data.hasreserve==0; 
        T   = sum(idx);
        probsh = @(H,n)   n.*(n-1).*((CDF(H).^(n-2)).*(1-CDF(H)).*PDF(H)) ;
        SHmat = ones(T,PAR.maxn+1);
        for n = 0:PAR.maxn
            SHmat(1:T,n+1)  = probsh(data.Uhat(idx),n);
        end

        % prob potential bidders given lambdastar
        pdfs_nbidders  = pdf('pois',0:PAR.maxn,lambda); 
        Pmat      = repmat(pdfs_nbidders,T,1);

        % joint likelihood;  (probsh=NaN for nrbidders =0,1 but these values will correctly be set equal to 1 below)
        jointprobobs = sum(ones(T,PAR.maxn+1).*SHmat.*Pmat,2);
        LL = - sum(log(jointprobobs));
        
    catch %generalized gaussian sensitive to shape parameter
        LL = Inf;
    end
            
end
